/**
 * Created by Administrator on 2016/12/27.
 */



var data =[
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆1", pid:1},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆2", pid:2},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆3", pid:3},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆4", pid:4},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆5", pid:5},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆6", pid:6},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆7", pid:7},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆8", pid:8},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆9", pid:9},
    {src:'images/prizeClaw/jd175_135.png', text:"中奖金豆10", pid:10}
]



function loding(dom,num,fn) {
    $(dom).animate({width:num},50,function () {
        if(fn) fn()
    })


}


$(document).ready(function () {


/*礼品滚动*/
var Roll =function (opt) {
    var self=this
    this.init=false
    this.gifts=[];
    this.gift={}
    this.opt =opt;
    this.giftImgIndx=0
    this.giftImgLen=opt.data.length
    this.clientWidth=$(window).width()
    this.clientHeight=$(window).height()
    // this.axis=opt.axis

    this.rangeMax=this.opt.axis+this.opt.range
    this.rangeMin=this.opt.axis-this.opt.range

    this.wrangeMax=this.rangeMax+this.opt.wrange
    this.wrangeMin=this.rangeMin-this.opt.wrange
    console.log(this.wrangeMax)
    console.log(this.wrangeMin)
    if(!(this.wrangeMin > 0 && this.wrangeMax < this.clientWidth)){
        // console.log("范围必须大于0,小于屏幕宽度")
        throw "伪范围必须大于0,小于屏幕宽度";
    }

    this.speed = opt.speed || 2;
    this.isCreate=true
    //是否能开始
    this.isSatrt=false
    /*爪子*/
    this.grabberTimer=null;
    this.grabberSpeed=5;
    this.grabber=0
    this.grabberTop=0
    //抓住礼品的存放位置
    this.graspgift=null
    this.wgraspgift=null
    /*媒体监听*/
    if(this.clientWidth <= 320){
        this.gift.w=100
        this.gift.h=105
        this.gift.gap=20
        this.gift.onset=-100
    }else {
        this.gift.w=120
        this.gift.h=125
        this.gift.gap=20
        this.gift.onset=-120
    }


    this.giftBox = $(opt.giftBox)
    this.gbr = $(opt.grabber)       //爪子


        setInterval(function () {

            if(self.isCreate)  {
                self.giftImgIndx++
                if(self.giftImgIndx >= self.giftImgLen){
                    self.giftImgIndx=0
                }

                var createGift =$('<div class="gift"' +
                    ' data-text="'+ self.opt.data[self.giftImgIndx].text+'"' +
                    ' data-src="'+ self.opt.data[self.giftImgIndx].src+'"' +
                    ' data-pid="'+self.opt.data[self.giftImgIndx].pid+'">' +
                    '<img src="images/prizeClaw/temp-0'+ self.getRandomNum(1,3)+'.png"/>' +
                    '</div>')

                self.gifts.unshift({
                    l:self.gift.onset,
                    g:createGift
                });
                createGift.css({
                    width:self.gift.w,
                    height:self.gift.h,
                    left:self.gift.onset
                })

                self.giftBox.prepend(createGift)
                self.isCreate =false
                if(!self.init){
                    console.log(createGift.offset())

                    self.grabber =Math.floor(createGift.offset().top-10)
                    self.grabberTop=-self.grabber*0.8
                    self.gbr.css({
                        left:self.opt.axis,
                        top:self.grabberTop,
                        height:self.grabber
                    });
                    self.init=true
                }
            }

            if(self.gifts[0].l>self.gift.gap){
                self.isCreate =true
            }
            if(self.gifts !== 0){
                for(var i=0; i<self.gifts.length; i++){
                    self.gifts[i].l += self.speed
                    self.gifts[i].g.css({
                        width:self.gift.w,
                        height:self.gift.h,
                        left:self.gifts[i].l
                    })

                    /*超出屏幕删除*/
                    if(self.containsPoint(self.gifts[i].l,self.clientWidth)){
                        self.gifts[i].g.remove()
                        self.gifts.splice(i,1)

                    }

                }
            }

        },30)



};
    Roll.prototype.satrt=function (scb) {
        var self=this
        var btr=true
        var gbrTop=self.grabberTop
        var turn =false;
        var grasp=null
        var wgrasp=null
        if(self.graspgift && !self.isSatrt){
            self.graspgift.remove()
            self.graspgift=null
        }
        if(self.wgraspgift){
            self.wgraspgift.remove()
            self.wgraspgift=null
        }
        self.gbr.addClass("act")


        if(!self.isSatrt){
            self.isSatrt=true
            self.grabberTimer=setInterval(function () {
                // console.log(gbrTop)
                if(gbrTop < 0 && btr){
                    gbrTop += self.grabberSpeed
                }else{
                    gbrTop-=self.grabberSpeed
                    self.gbr.removeClass("act")
                    if(!turn){
                        console.log("1次机会")
                        for(var j=0; j<self.gifts.length; j++){
                            var gA =self.gifts[j].l+self.gift.w*0.5;

                            if(self.wrangeMin<gA && gA<self.wrangeMax){
                                if(self.rangeMin<gA && gA<self.rangeMax){
                                    //请求请求请求请求请求请求请求请求*****
                                    var giftOffset=self.gifts[j].g.offset()
                                    //礼品队列删除 礼品
                                    self.graspgift=grasp=self.gifts[j].g.addClass("act").clone().css({
                                        left:"50%",
                                        marginLeft:-self.gift.w*0.5,
                                        bottom:-self.gift.h
                                    })
                                    self.gbr.append(grasp)
                                    self.gifts[j].g.remove()
                                    self.gifts.splice(j,1)

                                }else {
                                    console.log("伪中奖")
                                    self.wgraspgift=self.gifts[j].g.clone().css({
                                        left:"50%",
                                        marginLeft:-self.gift.w*0.5,
                                        bottom:-self.gift.h
                                    })
                                    /*var gravity = self.gift.h
                                    gravity+=6*/
                                    setTimeout(function () {
                                        self.wgraspgift.animate({
                                            bottom:-self.clientHeight
                                        },1000,function () {
                                            self.wgraspgift.remove()
                                            self.wgraspgift=null
                                        })
                                    },500)
                                    self.gbr.append(self.wgraspgift)
                                    self.gifts[j].g.remove()
                                    self.gifts.splice(j,1)
                                }


                            }

                        }

                        turn=true
                    }
                    if(gbrTop <= self.grabberTop){
                        clearInterval(self.grabberTimer)
                        self.isSatrt=false
                        scb(grasp)
                    }
                    btr=false

                }

                self.gbr.css({
                    top:gbrTop
                })
            },30)
        }


    }

    Roll.prototype.getRandomNum=function(Min,Max)
    {
        var Range = Max - Min;
        var Rand = Math.random();
        return(Min + Math.round(Rand * Range));
    }
    Roll.prototype.containsPoint=function (rectW, winW) {
        return !(winW > rectW);
    }


loding("#degree" ,0)
function init() {


    //基础
    var CONFIG = {
        giftBox:".gifts-wrap",      //滚动元素
        gift:".gift",
        grabber:".grabber",
        axis:$(window).width()/2,
        range:5 ,        //容错范围
        wrange:20 ,        //伪范围
        data:data,
        speed:4
    };

    var zjHtml=function (url,text) {

        return '<div class="jp-layer">' +
            '<h4>恭喜您获得</h4>' +
            '<img src="'+url+'" />' +
            '<p>'+text+'</p>' +
            '</div>'
    }
    var docEl = document.documentElement,
    recalc = function (fn) {
        var clientWidth = docEl.clientWidth;
        if (!clientWidth) return;
        docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
        fn()
    };
    loding("#degree" ,"50%")
    recalc(function () {

        loding("#degree" ,"100%",function () {
            $(".loading").fadeOut()
             });

        var roll = new Roll(CONFIG)
        $("#start").off("click").on("tapone",function () {
            var $self=$(this)
            $(this).addClass("active")
            setTimeout(function () {
                $self.removeClass("active")
            },100)
            roll.satrt(function (d) {
                if(d){
                    layer.open({
                        className:"jp-layer-box",
                        content: zjHtml(d.attr("data-src"),d.attr("data-text"))
                    });
                }

            })

        })
    });




    
}

$(window).resize(function () {

    location.replace(location.href);
});

init();


})